package com.smart.business.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smart.business.dao.CompanyDao;
import com.smart.business.dao.LocalDictDao;
import com.smart.common.utils.ThreadLocalUtil;
import com.smart.entity.business.CompanyBarVO;
import com.smart.model.exception.SmartException;
import com.smart.mybatis.service.impl.BaseServiceImpl;
import com.smart.business.dao.PointLocationDao;
import com.smart.entity.business.PointLocationEntity;
import com.smart.service.business.PointLocationService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class PointLocationServiceImpl extends BaseServiceImpl<PointLocationDao,PointLocationEntity> implements PointLocationService {
    @Autowired
    private PointLocationDao pointLocationDao;

    /**
     * 计算点位离线率，在线率，异常率
     * 0 1 2 正常 ,3 离线 , 4 异常
     */
    @Override
    public List<Map<String, Object>> getPointPie() {
        //计算点位离线率，在线率，异常率
       List<String> companyNames = ThreadLocalUtil.getPermissionResult();
       if (companyNames == null || companyNames.isEmpty()) {
           throw new SmartException("公司名称不能为空");
       }
        // 查询所有有效点位数据
        List<Map<String,Object>> pointList = pointLocationDao.getAllPoints(companyNames);

        // 初始化统计变量
        int offlineCount = 0;
        int onlineUsedCount = 0;
        int abnormalCount = 0;
        int notUsedCount = 0;
        for (Map<String, Object> point : pointList) {
            String status = point.get("ano_status").toString();
            if ("0".equals(status) || "1".equals(status) || "2".equals(status)) {
                onlineUsedCount++;
            } else if ("3".equals(status)) {
                offlineCount++;
            }else if ("4".equals(status)) {
                abnormalCount++;
            }else if ("5".equals(status)) {
                notUsedCount++;
            }
        }

        // 构建返回结果
        List<Map<String, Object>> result = new ArrayList<>();
        double total = offlineCount + onlineUsedCount + abnormalCount;

        if (offlineCount > 0) {
            Map<String, Object> offlineMap = new HashMap<>();
            offlineMap.put("name", "离线");
            offlineMap.put("value", offlineCount);
            offlineMap.put("percentage", formatPercentage(offlineCount, total));
            result.add(offlineMap);
        }


        if (onlineUsedCount > 0) {
            Map<String, Object> usedMap = new HashMap<>();
            usedMap.put("name", "在线");
            usedMap.put("value", onlineUsedCount);
            usedMap.put("percentage", formatPercentage(onlineUsedCount, total));
            result.add(usedMap);
        }


        if (abnormalCount > 0) {
            Map<String, Object> abnormalMap = new HashMap<>();
            abnormalMap.put("name", "异常");
            abnormalMap.put("value", abnormalCount);
            abnormalMap.put("percentage", formatPercentage(abnormalCount, total));
            result.add(abnormalMap);
        }
        if (notUsedCount > 0) {
            Map<String, Object> notUsedMap = new HashMap<>();
            notUsedMap.put("name", "在线未使用");
            notUsedMap.put("value", notUsedCount);
            notUsedMap.put("percentage", formatPercentage(notUsedCount, total));
            result.add(notUsedMap);
        }

        return result;
    }
    // 格式化百分比，保留两位小数
    private String formatPercentage(double part, double total) {
        if (total == 0) return "0%";
        double percentage = Math.round((part / total) * 10000) / 100.0;
        return percentage + "%";
    }

    @Override
    public List<Map<String,Object>> getAllCompanyPointStatus() {
        List<String> companyNames = ThreadLocalUtil.getPermissionResult();
        if (companyNames == null || companyNames.isEmpty()) {
            throw new IllegalArgumentException("公司名称不能为空");
        }

        return pointLocationDao.getAllCompanyPointStatus(companyNames);
    }

    @Override
    public List<Map<String,Object>> getCompanyPointBar() {
        List<String> companyNames = ThreadLocalUtil.getPermissionResult();
        if (companyNames == null || companyNames.isEmpty()) {
            return Collections.emptyList();
        }
        return pointLocationDao.getCompanyPointBar(companyNames);
    }

    @Override
    public List<Map<String, Object>> getCompanyLocation() {
        List<String> companyNames = ThreadLocalUtil.getPermissionResult();
        if (companyNames == null || companyNames.isEmpty()) {
            return Collections.emptyList();
        }
        return pointLocationDao.getCompanyLocation(companyNames);
    }





}

